import matplotlib.pyplot as plt
from fmeter import TransactionAnalyzer
import copy
import numpy as np
import sys
import pdb

params = {
    'lines.linewidth': 0.5,
    'font.family': "sans-serif",
    'font.sans-serif': ['Euclid'],
    'font.size': 7,
    'xtick.labelsize': 7,
    'ytick.labelsize': 7,
    'axes.grid': True,
    'grid.linestyle': '--',
    'legend.fontsize': 7,
    'figure.figsize': [2.3622, 2],
    'savefig.dpi': 600,
    'savefig.bbox': 'tight'
}

if __name__ == '__main__':

    if len(sys.argv) != 3:
        print("usage: {} data.h5 block|delay".format(sys.argv[0]))
        sys.exit()
    paramsLocal = copy.deepcopy(params)
    paramsLocal["figure.figsize"] = [6.29921, 2]
    plt.rcParams.update(paramsLocal)
    plt.clf()

    ta = TransactionAnalyzer()
    ta.load(sys.argv[1])
    if sys.argv[2] == "block":
        # 块大小
        y = ta.trans_block_distribution
        print("mean: {}, std: {}, max: {}, min: {}\n".format(y.mean(), y.std(), y.max(), y.min()))
        # 横轴为序号
        names = range(len(y))
        # ly = len(y)

        # block = ly // 10
        # y = [np.mean(y[i * block:(i + 1) * block]) for i in range(10)]
        # pdb.set_trace()
        plt.bar(names, y)
        # # 分布情况
        # # plt.hist(y, density=True)
        plt.show()
    elif sys.argv[2] == "delay":
        # 出块延迟
        y = ta.transaction_record_df["takes"].dt.total_seconds()
        print("mean: {}, std: {}, max: {}, min: {}\n".format(y.mean(), y.std(), y.max(), y.min()))
        block = len(y) // 100
        y = [np.mean(y[i * block:(i + 1) * block]) for i in range(100)]
        plt.bar(range(len(y)), y)
        # 出块延迟分布情况
        # plt.hist(y, density=True)
        # plt.gca().set_yscale("log")
        plt.show()
